package com.budayi.common.config;

import com.budayi.common.filter.BodyReaderFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.DispatcherType;
import javax.servlet.Filter;

/**
 * @author LIBOFEI
 * @ClassName BodyFilterConfig
 * @description
 * @date 2021/11/4 11:12
 * @Version 1.0
 **/
@Configuration
public class BodyFilterConfig {

    @Bean
    public FilterRegistrationBean filterRegistrationBodyFilterBean() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(bodyFilter());
        registration.addUrlPatterns("/*");

        registration.setDispatcherTypes(DispatcherType.REQUEST);
        registration.setName("bodyRequestFilter");
        return registration;
    }

    /**
     * 创建一个bean
     * @return
     */
    @Bean(name = "bodyFilter")
    public Filter bodyFilter() {
        return new BodyReaderFilter();
    }
}
